package com.sgb.auction.viewmodule;

import android.view.View;
import androidx.lifecycle.ViewModel;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.sgb.auction.R;
import com.sgb.auction.activity.AuctionPublishActivity;
import com.sgb.auction.base.BaseApi;
import com.sgb.auction.entity.AuctionPublishBean;
import com.sgb.auction.entity.MapLongAndLatEntity;
import com.sgb.auction.entity.PublishListBean;
import com.sgb.link.entity.BrandEntity;
import com.sgb.link.entity.GetSkuEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.GlideEngine;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.entity.MenuEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Time:2021/11/19
 * AUTHOR:F_Q
 * Description:
 */
public class AuctionPublishModel extends ViewModel {
    AuctionPublishActivity activity;

    public void setActivity(AuctionPublishActivity auctionPublishActivity) {
        this.activity = auctionPublishActivity;
    }

    //申请单位列表
    public void getCompanyList(View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMyCompanyList().enqueue(new ICallBack<BaseEntity<List<PublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setCompanyList(data.getData(), view);
                    } else {
                        MToast.showToast(activity, "当前用户没有其它公司信息");
                    }
                }
            }
        });
    }

    //申请部门列表
    public void getDepartmentList(String applyCompany, View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getDeptList(applyCompany).enqueue(new ICallBack<BaseEntity<List<PublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setDepartmentList(data.getData(), view);
                    } else {
                        MToast.showToast(activity, "当前单位无部门信息");
                    }
                }
            }
        });
    }

    //地址列表信息
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    activity.setAddressList(data.getData());
                }
            }
        });
    }

    // 获取经纬度信息
    public void getLocationLL(String provinceNo, String cityNo, String areaNo) {
        String areaCode = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.AppProvinceCode, "");
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getLocationLL(areaCode, provinceNo, cityNo, areaNo).enqueue(new ICallBack<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onSuccess(BaseEntity<MapLongAndLatEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.updateAddress(data.getData());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 获取商品列表
     */
    public void getGoodsCategory() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getGoodsTreeNodeByProvinceIdV2().enqueue(new ICallBack<BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity<MenuEntity<MenuEntity>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.showGoodsTypePop(data.getData());
                }
            }
        });
    }

    /**
     * 品牌范围
     */
    public void getRangeBrandsList(String thirdType, View view) {
        String companyId = SharedPreferenceUtil.getInstance().getSaveStringData(com.swgk.core.util.SharedPreferenceUtil.REAL_ID, "");
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getBrandList(thirdType, "", companyId).enqueue(new ICallBack<BaseEntity<List<BrandEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<BrandEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null) {
                    return;
                }
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.showBrandPop(data.getData(), view);
                }
            }
        });
    }

    /**
     * 是否缴纳质保金
     *
     * @param view
     */
    public void getIsmarginList(View view) {
        List<PublishListBean> listBean = new ArrayList<>();
        PublishListBean ismarginlist1 = new PublishListBean();
        ismarginlist1.setName("是");
        ismarginlist1.setId("1");
        listBean.add(ismarginlist1);
        PublishListBean ismarginlist2 = new PublishListBean();
        ismarginlist2.setName("否");
        ismarginlist2.setId("2");
        listBean.add(ismarginlist2);
        activity.setIsmarginList(listBean, view);
    }

    /**
     * 支付方式
     *
     * @param title
     * @param view
     */
    public void getPaymentList(String title, View view) {
        List<PublishListBean> listBean = new ArrayList<>();
        PublishListBean ismarginlist1 = new PublishListBean();
        ismarginlist1.setName("线下支付");
        ismarginlist1.setId("2");
        listBean.add(ismarginlist1);
        PublishListBean ismarginlist2 = new PublishListBean();
        ismarginlist2.setName("网上支付（暂未开通）");
        ismarginlist2.setId("1");
        listBean.add(ismarginlist2);
        activity.setPaymentList(listBean, view, title);
    }

    /**
     * 运输方式
     *
     * @param view
     */
    public void getTransportType(View view) {
        //运输方式（1、商家配送,2、需方自提）
        List<PublishListBean> listBean = new ArrayList<>();
        PublishListBean ismarginlist1 = new PublishListBean();
        ismarginlist1.setName("商家配送");
        ismarginlist1.setId("1");
        listBean.add(ismarginlist1);
        PublishListBean ismarginlist2 = new PublishListBean();
        ismarginlist2.setName("需方自提");
        ismarginlist2.setId("2");
        listBean.add(ismarginlist2);
        activity.setTransportType(listBean, view);
    }

    public void pictureSelector(int number, int requestCode) {
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .loadImageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.picture_default_style)
                .selectionMode(PictureConfig.MULTIPLE)
                .compress(true)//是否压缩
                .minimumCompressSize(3413)//小于3m不压缩
                .maxSelectNum(number)//判断还能选多少张图片
                .forResult(requestCode);
    }

    //上传图片
    public void uploadAvatarFileModel(File file, int completeNum) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "上传中" + "...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    MToast.showToast(activity, "上传失败！");
                    return;
                }
                if (response.body().getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.uploadAvatarResult(response.body().getData().toString());
                } else {
                    MToast.showToast(activity, "上传失败！");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "上传失败！" + t.getMessage());
            }
        });
    }

    //获取当前商品的sku属性
    public void getSku(String cid) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getSku(cid).enqueue(new ICallBack<BaseEntity<List<GetSkuEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<GetSkuEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals("ok")) {
                    activity.isSku(data.getData());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 提交数据心爱
     *
     * @param bean
     */
    public void AddAuction(AuctionPublishBean bean) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddAuction(bean).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals("ok")) {
                    activity.setAddAuction(data.getMsg());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 获取计量单位列表
     *
     * @param view
     */
    public void getUnitList(View view) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getfindUnitList().enqueue(new ICallBack<BaseEntity<List<PublishListBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PublishListBean>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().size() > 0) {
                        activity.setUnitList(data.getData(), view);
                    } else {
                        MToast.showToast(activity, "没有单位信息");
                    }
                }
            }
        });
    }

    /**
     * 获取会员等级列表
     *
     * @param view
     */
    public void getMemberLevelList(View view) {
        /**
         * { no: 0, label: "不限" },
         *                 { no: 1, label: "1" },
         *                 { no: 2, label: "2" },
         *                 { no: 3, label: "3" },
         *                 { no: 4, label: "4" },
         *                 { no: 5, label: "5" }
         */
        List<PublishListBean> listBean = new ArrayList<>();
        PublishListBean ismarginlist = new PublishListBean();
        ismarginlist.setName("不限");
        ismarginlist.setId("0");
        listBean.add(ismarginlist);
        PublishListBean ismarginlist1 = new PublishListBean();
        ismarginlist1.setName("1星及以上");
        ismarginlist1.setId("1");
        listBean.add(ismarginlist1);
        PublishListBean ismarginlist2 = new PublishListBean();
        ismarginlist2.setName("2星及以上");
        ismarginlist2.setId("2");
        listBean.add(ismarginlist2);
        PublishListBean ismarginlist3 = new PublishListBean();
        ismarginlist3.setName("3星及以上");
        ismarginlist3.setId("3");
        listBean.add(ismarginlist3);
        PublishListBean ismarginlist4 = new PublishListBean();
        ismarginlist4.setName("4星及以上");
        ismarginlist4.setId("4");
        listBean.add(ismarginlist4);
        PublishListBean ismarginlist5 = new PublishListBean();
        ismarginlist5.setName("5星及以上");
        ismarginlist5.setId("5");
        listBean.add(ismarginlist5);
        activity.setMemberLevelList(listBean, view);
    }

    ;

    /**
     * 竞买人身份
     *
     * @param view
     */
    public void getBiddingStatusList(View view) {
        //竞标人身份设定 1 公司，个人皆可 2 仅限公司
        List<PublishListBean> listBean = new ArrayList<>();
        PublishListBean ismarginlist1 = new PublishListBean();
        ismarginlist1.setName("公司、个人皆可");
        ismarginlist1.setId("1");
        listBean.add(ismarginlist1);
        PublishListBean ismarginlist2 = new PublishListBean();
        ismarginlist2.setName("仅限公司");
        ismarginlist2.setId("2");
        listBean.add(ismarginlist2);
        activity.setBiddingStatusList(listBean, view);
    }

    /**
     * 是否公示竞买人名称
     *
     * @param view
     */
    public void getBiddingPublicList(View view) {
        //竞买人名称是否公示（1、是,2、否）
        List<PublishListBean> listBean = new ArrayList<>();
        PublishListBean ismarginlist1 = new PublishListBean();
        ismarginlist1.setName("不公示");
        ismarginlist1.setId("2");
        listBean.add(ismarginlist1);
        PublishListBean ismarginlist2 = new PublishListBean();
        ismarginlist2.setName("公示");
        ismarginlist2.setId("1");
        listBean.add(ismarginlist2);
        activity.setBiddingPublicList(listBean, view);
    }

    /**
     * 竞买人限定
     */
    public void getBidderLimitedlicList() {
        //	竞买人限定 1 按区域 2按会员星级
        List<PublishListBean> listBean = new ArrayList<>();
        PublishListBean ismarginlist1 = new PublishListBean();
        ismarginlist1.setName("限会员");
        ismarginlist1.setId("2");
        listBean.add(ismarginlist1);
        PublishListBean ismarginlist2 = new PublishListBean();
        ismarginlist2.setName("限企业注册地区");
        ismarginlist2.setId("1");
        listBean.add(ismarginlist2);
        activity.setBidderLimitedlicList(listBean);
    }

    /**
     *获取注册地区接口
     */
    public void getAcceptAreaList() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMarketArea().enqueue(new ICallBack<BaseEntity<BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    if (data.getData().getData().size()> 0) {
                        activity.setAcceptAreaList(data.getData().getData());
                    }
                }
            }
        });
    }
}
